<?php
require('crawler.class.php');
require('config.php');
require('phpQuery/phpQuery/phpQuery.php');
$type = isset($_GET['response']) ? $_GET['response'] : 'form';

$sql = "SELECT * FROM sites WHERE id=" . intval($_GET['id']);
$rs = mysql_query($sql);
if($row = mysql_fetch_array($rs)):
	$url = str_replace(' ', '%20', $row['home_url']);
	$filterUrl = $row['link_filter_type'];
	$urlAdd = $row['link_before'];
	$filterString = base64_decode($row['link_filter_string']);

	$filterDetail = $row['detail_filter_type'];
	$filterContentString = base64_decode($row['filter_string']);
	$removeDom = $row['dom_remove'];
	
	$limitUrl = intval($row['limit']);
	$crawler = new Crawler($url, $limitUrl);
	$links = $crawler->getLinks($filterUrl, $filterString, array(
				'detailType' => $filterDetail,
				'detailString' => $filterContentString,
				'removeDom' => $removeDom,
				'urlAdd' => $urlAdd,
				'demo' => false)
	);
	if($type == 'json'){
		header('Content-Type: application/json');
		echo json_encode($links['detail']);exit(0);
	}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
	    <!-- Meta, title, CSS, favicons, etc. -->
	    <meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="description" content="pureCrawler by thaodx@gmail.com.">
		<meta name="keywords" content="crawler, pureCrawler, content grabber">
		<meta name="author" content="ThaoDX">

		<title>pureCrawler</title>
		<!-- Latest compiled and minified CSS -->
		<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
		<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
		<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
		<!-- Optional theme -->
		<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
		<!-- Latest compiled and minified JavaScript -->
		<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
		<script src="ckeditor/ckeditor.js"></script>
		<style type="text/css">
		body {
			padding-top: 70px;
		}
		#modal-body{
			max-height: 400px;
			overflow: auto;
		}
		.smallfont{
			font-size:11px !important;
			font-family: courier;
		}
		.extract-detail{
			cursor: pointer;
		}
		</style>
		<script type="text/javascript">
		$(document).ready(function(){

			
		});
		</script>
	</head>
	<body>
		<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
		  <div class="container-fluid">
		    <!-- Brand and toggle get grouped for better mobile display -->
		    <div class="navbar-header">
		      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
		        <span class="sr-only">Toggle navigation</span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		      </button>
		      <a class="navbar-brand" href="/index.php"><span class="glyphicon glyphicon-home"></span> Home</a>
		    </div>
		  </div><!-- /.container-fluid -->
		</nav>

		<div class="container-fluid">
			<?php if(!empty($links)): ?>
			<?php $i = 0; ?>
			<?php foreach($links['detail'] as $key => $detail): ?>
			<div class="row">
				<div class="col-md-8">
					<form class="form-horizontal" role="form">
						  <div class="form-group">
						    <label for="name" class="col-sm-4 control-label">Title</label>
						    <div class="col-sm-8">
						      <input type="text" name="title" class="form-control" id="title" value="<?php echo htmlentities($detail['title'], ENT_COMPAT, 'UTF-8') ?>">
						    </div>
						  </div>

						  <div class="form-group">
						    <label for="name" class="col-sm-4 control-label">Source <a href="<?php echo $links['links'][$i] ?>" title="Open source URL in new window" target="_blank"><span class="glyphicon glyphicon-new-window"></span></a></label>
						    <div class="col-sm-8">
						      <input type="text" value="<?php echo $links['links'][$i] ?>" name="source" class="form-control" id="title" disabled>
						    </div>
						  </div>

						  <div class="form-group">
						    <label for="content" class="col-sm-4 control-label">&nbsp;</label>
						    <div class="col-sm-8">
						      <textarea class="ckeditor" name="content"><?php echo $detail['body'] ?></textarea>
						    </div>
						  </div>

					</form>
				</div>
				<div class="col-md-3">
					<div class="panel panel-default">
						<div class="panel-heading">
							<h3 class="panel-title">Post meta</h3>
						</div>
						<div class="container-fluid" style="margin:10px 0;">
							<div class="checkbox">
							  <label>
							    <input type="checkbox" value="">
							    Category #1
							  </label>
							</div>
							<div class="checkbox">
							  <label>
							    <input type="checkbox" value="">
							    Category #2
							  </label>
							</div>
							<div class="checkbox">
							  <label>
							    <input type="checkbox" value="">
							    Category #3
							  </label>
							</div>
							<div class="form-group">
								<textarea class="form-control" rows="3" placeholder="Post Tags"></textarea>
							</div>
							<div class="form-group">
								<input type="text" class="form-control" id="home_url" placeholder="Thumbnail URL">
							</div>
							<div class="form-group">
						      <button type="submit" id="btn-save" class="btn btn-primary"><span class="glyphicon glyphicon-share"></span> Submit post</button>
						  </div>
						</div>
					</div>
				</div>
			</div>
			<!--/ post form -->
			<?php $i++; ?>
			<?php endforeach; ?>
			<?php endif; ?>
		</div>
		<?php endif; ?>
	</body>
</html>